/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import java.sql.*;
import java.util.Vector;
import Beans.ThamSoHT;

/**
 *
 * @author VIC
 */
public class ThamSoHT_Model {

    public ThamSoHT_Model() {
    }

    public static ThamSoHT[] convertToArray(Vector v) {
        int n = v.size();
        ThamSoHT[] arr = new ThamSoHT[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (ThamSoHT) v.get(i);
        }
        return arr;
    }
    /* HÃ m nÃ y tráº£ vá» danh sÃ¡ch cÃ¡c tham sá»‘ theo ngÃ´n ngá»¯ Ä‘Æ°á»£c chá»‰ Ä‘á»‹nh
     * tham sá»‘ : dsts : danh sÃ¡ch chá»©a mÃ£ cÃ¡c tham sá»‘
     * tham sá»‘ : mann : mÃ£ ngÃ´n ngá»¯
     */

    public static ThamSoHT[] DSThamSo(String dsts, int mann) {

        Vector vdstso = new Vector();
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            query = " Select * from ThamSoHeThong where mann = " + mann + " and mats in (" + dsts + ") Order by mats asc ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                ThamSoHT ts = new ThamSoHT();
                ts.setMaTS(rss.getString(1));
                ts.setMaNN(rss.getInt(2));
                ts.setTenTS(rss.getString(3));
                vdstso.add(ts);
            }
            stmt.close();
            rss.close();
            conn.close();
            return convertToArray(vdstso);
            
        } catch (Exception e) {
            return null;
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }
    /* HÃ m tráº£ vá» tÃªn tham sá»‘ á»©ng vá»›i ngÃ´n ngá»¯ yÃªu cáº§u.
     * tham sá»‘ : mÃ£ tham sá»‘, mÃ£ ngÃ´n ngá»¯
     * tráº£ vá» : tÃªn tham sá»‘ theo ngÃ´ ngá»¯ yÃªu cáº§u
     */

    public static String TenThamSoHT(String mats, int mann) {

        String tents = "";// biáº¿n tents lÆ°u tÃªn tham sá»‘ tráº£ vá».
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            query = "Select tents from ThamSoHeThong where mann = " + mann + " and mats = '" + mats + "'";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                tents = rss.getString(1);
            }
            stmt.close();
            rss.close();
            conn.close();
            return tents;
        } catch (Exception e) {

            return "";
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }

    }
}
